#coding=utf-8
import os
import datetime
import time
import sys
import glob
from chardet.universaldetector import UniversalDetector

# 字节bytes转化kb\m\g
def formatSize(bytes):
    try:
        bytes = float(bytes)
        kb = bytes / 1024
    except:
        print("传入的字节格式不对")
        return "Error"

    if kb >= 1024:
        M = kb / 1024
        if M >= 1024:
            G = M / 1024
            return "%fG" % (G)
        else:
            return "%fM" % (M)
    else:
        return "%fkb" % (kb)

# 获取文件大小
def getDocSize(path):
    try:
        size = os.path.getsize(path)
        return formatSize(size)
    except Exception as err:
        print(err)

def GetEncode( filename ):
        size = os.path.getsize(filename)
        print(size)
    
        try:  
                f=open(filename,'rb')
                b= b' '
                b+=f.read(int(size/10))
                u=UniversalDetector()
                u.reset()
                u.feed(b)
                u.close()
                f.seek(0)
                b=f.read()
                f.close()
                return u.result['encoding']
        except IOError: 
                print ("GetEncode Error: " )
 

def ToDateTime( yearStr, dateTimeStr ):
        #print(dateTimeStr)
        return datetime.datetime.strptime(yearStr+" "+ dateTimeStr, "%Y %b %d %H:%M:%S")
        arr = dateTimeStr.split(' ')
        yearStr += "-"+str(ToNumber( arr[0] ))+"-"+arr[1]+" "+arr[2]
        return datetime.datetime.strptime(yearStr, "%Y-%m-%d %H:%M:%S")
		

def GetBreakPoint( intervalTime, curFileName ):	
        #intervalTime = 10
        #curFileName = 'all1.txt'
        fileEncoding = GetEncode( curFileName )
        print(fileEncoding)
        
        with open( curFileName, encoding=fileEncoding) as f:
                #f = open('all1.txt', encoding="utf-8")
                lineList = f.readlines()

                curLines = 0
                TotalLines = len(lineList)
                while(curLines < TotalLines):
                    line1Arr = lineList[curLines].split('\t')
                    #print(len(arr))
                    time1 =ToDateTime( "2018", line1Arr[1] )
                    #print(time1)

                    if(curLines+1 < TotalLines):
                        line2Arr = lineList[curLines+1].split('\t')
                        time2 =ToDateTime( "2018", line2Arr[1] )

                        #print(type(time2  - time1) )
                        if((time2  - time1).total_seconds() > intervalTime ):
                            print(curFileName +" "+str(curLines+1) + ":"+str(time2) )
                            
                    curLines+=1
        print("end")


##将内容写入文件中
def WriteTofile( beginLines, endLines, strList, fileName ):
        with open(fileName, "w", encoding="utf-8") as fp:
                #print(strList[beginLines:endLines])
                fp.writelines(strList[beginLines:endLines])
        print("save to:",fileName)

##回溯提取最近的一次修复动作
def GetLastRepear(curLines, lineList, newFileName):
        restartLine = curLines
        while(restartLine >= 0):
                restartLine -= 1
                if(restartLine >= 0):
                        line3Arr = lineList[restartLine].split('\t')
                        if( line3Arr[6].find("开始进程") >= 0 ):
                                #newFileName = curFileName+"_"+str(curLines)+"_"+ToDateTime( "2018", line1Arr[1] ).strftime("%Y-%m-%d-%H-%M-%S")+ ".txt"
                                print(restartLine,";", curLines,";",newFileName)
                                WriteTofile( restartLine, curLines, lineList, newFileName )
                                break


def GetFromTime( TimePoint, curFileName ):	
        #intervalTime = 10
        #curFileName = 'all1.txt'
        dateTimePoint = datetime.datetime.strptime(TimePoint,"%Y_%m_%d_%H_%M_%S")
        fileEncoding = GetEncode( curFileName )
        print(fileEncoding)
        
        with open( curFileName, encoding=fileEncoding, errors='ignore' ) as f:
                #f = open('all1.txt', encoding="utf-8")
                lineList = f.readlines()

                curLines = 0
                TotalLines = len(lineList)
                while(curLines < TotalLines):
                    line1Arr = lineList[curLines].split('\t')
                    #print(len(arr))
                    time1 =ToDateTime( "2018", line1Arr[1] )
                    #print(time1)

                    if(time1 > dateTimePoint):
                        WriteTofile( curLines, TotalLines, lineList, curFileName+"_From_"+TimePoint )
                        break
                            
                    curLines+=1
        print("end")

                

def GetBreakRestartPoint( intervalTime, curFileName ):	
        #intervalTime = 10
        #curFileName = 'all1.txt'
        fileEncoding = GetEncode( curFileName )
        print(fileEncoding)
            
        with open( curFileName, "r", encoding=fileEncoding) as f:
        #with open( curFileName, "r",encoding='gb2312', errors='ignore')  as f:
        #with open( curFileName, "r",encoding='latin-1')  as f:
                lineList = f.readlines() 

                curLines = 0
                TotalLines = len(lineList)
                print("TotalLines:",TotalLines)
                
                while(curLines < TotalLines):
                        #print("curLines:",curLines)
                        line1Arr = lineList[curLines].split('\t')
                        #print(line1Arr[6])

                        if( line1Arr[6].find("AlarmLog 构造") >= 0 ):  ##软件重启点
                        #if( line1Arr[6] == "AlarmLog 构造" ):
                                #print(line1Arr[6], curLines)
                                if(curLines-1 >= 0):
                                        line2Arr = lineList[curLines-1].split('\t')

                                        if( (line2Arr[6].find("AlarmLog 析构") == -1)
                                            and (line2Arr[6].find("RunLog 析构") == -1)
                                            and (line2Arr[6].find("RealeaseAd ok") == -1)
                                            and (line2Arr[6].find("AxisStopMoveAll ok") == -1)): ##软件不是正常关闭
                                                print( curLines-1, line2Arr[6] )
                                                newFileName = curFileName+"_"+str(curLines)+"_"+ToDateTime( "2018", line1Arr[1] ).strftime("%Y-%m-%d-%H-%M-%S")+ ".txt"
                                                GetLastRepear(curLines, lineList, newFileName)
                            
                        curLines+=1
        print("end")

#删除循环中已完成的log
def DeleteRepeatLog( curFileName, logStr, logLines, startLine ):	
        #intervalTime = 10
        #curFileName = 'all1.txt'
        fileEncoding = GetEncode( curFileName )
        print(fileEncoding)

    
        #with open( curFileName, "r", encoding="utf-8") as f:
        with open( curFileName, "r",encoding=fileEncoding, errors='ignore')  as f:
        #with open( curFileName, "r",encoding='latin-1')  as f:
                lineList = f.readlines() 

                curLines = 0
                TotalLines = len(lineList)
                print("TotalLines:",TotalLines)

                curRepeatLogLine = startLine
                RepeatLog = []
                #print(type(range(startLine,logLines)))
                curRepeatLog = list(range(startLine, (logLines-startLine)+2))
                
                while(curLines < TotalLines):
                        #print("curLines:",curLines)
                        line1Arr = lineList[curLines].split('\t')
                        #print(line1Arr[6])

                        if( (line1Arr[6].find(logStr)  >= 0)
                            and (line1Arr[6].find(logStr+str(curRepeatLogLine)) == -1) ):
                                        print("break point:",lineList[curLines])
                                        curRepeatLogLine = startLine
                                        curRepeatLog = list(range(startLine, (logLines-startLine)+2))
                                
                        if( line1Arr[6].find(logStr+str(curRepeatLogLine)) >= 0 ):
                                #print(len(curRepeatLog),",", curRepeatLogLine - startLine )
                                curRepeatLog[ curRepeatLogLine - startLine ] = curLines
                                
                                if((curRepeatLogLine+1)>logLines):
                                        curRepeatLogLine = startLine
                                        RepeatLog.append(curRepeatLog)
                                        curRepeatLog = list(range(startLine, (logLines-startLine)+2))
                                else:
                                    curRepeatLogLine += 1    
                                        

                        curLines+=1


                ##准备删除循环的日志
                #help(type(lineList))
                lenght0 = len(RepeatLog)
                curItemIndex = lenght0-1
                
                while(curItemIndex>=0):
                        #print("curItemIndex:", curItemIndex)
                        lenghtItem = len(RepeatLog[curItemIndex])
                        curLogIndex = lenghtItem-1
                        
                        while(curLogIndex>=0):
                                #print((RepeatLog[curItemIndex])[curLogIndex])
                                lineList.pop((RepeatLog[curItemIndex])[curLogIndex])
                                curLogIndex -= 1
                        
                        curItemIndex -= 1
                WriteTofile( 0, len(lineList), lineList, curFileName+ "_edit" )
                                

##                for item in RepeatLog:
##                        print(item)
##                        for lineNum in item:   
##                                print(lineList[lineNum])
                        
        print("end")



##检测指定顺序的日志,列出不一致的位置
def FindSortLog( SortLog, curFileName ):	
        #intervalTime = 10
        #curFileName = 'all1.txt'
        fileEncoding = GetEncode( curFileName )
        print(fileEncoding)
            
        with open( curFileName, "r", encoding=fileEncoding) as f:
        #with open( curFileName, "r",encoding='gb2312', errors='ignore')  as f:
        #with open( curFileName, "r",encoding='latin-1')  as f:
                lineList = f.readlines() 

                curLines = 0
                TotalLines = len(lineList)
                print("TotalLines:",TotalLines)

                SortLogLen = len(SortLog)
                
                SortLogIndex = 0
                
                print("SortLogLen:", SortLogLen, " SortLogIndex:",SortLogIndex)
                if(SortLogLen <= 0):
                    return 

                getLine = 0
                while(curLines < TotalLines):
                        #print("curLines:",curLines)
                        line1Arr = lineList[curLines].split('\t')
                        #print(line1Arr[6])

                        if( line1Arr[6].find(SortLog[SortLogIndex]) >= 0 ):
                            if( SortLogIndex+1 < SortLogLen ):
                                SortLogIndex += 1
                                getLine = curLines
                                curLines+=1
                                continue
                            else:
                                SortLogIndex = 0
                                curLines+=1
                                continue

                        if(line1Arr[6].find(SortLog[0]) >= 0 ):
                            print( getLine, ":", lineList[getLine] )
                            SortLogIndex = 0
                            
                        curLines+=1
        print("FindSortLog end")

##检测指定顺序的日志,列出不一致的位置
def FindSortLog1( SortLog, curFileName ):	
        #intervalTime = 10
        #curFileName = 'all1.txt'
        fileEncoding = GetEncode( curFileName )
        print(fileEncoding)
            
        with open( curFileName, "r", encoding=fileEncoding) as f:
                lineList = f.readlines() 

                curLines = 0
                TotalLines = len(lineList)
                print("TotalLines:",TotalLines)

                SortLogLen = len(SortLog)
                
                SortLogIndex = 0
                
                print("SortLogLen:", SortLogLen, " SortLogIndex:",SortLogIndex)
                if(SortLogLen != 2):
                    return 
                
                while(curLines < TotalLines):
                        #print("curLines:",curLines)
                        line1Arr = lineList[curLines].split('\t')
                        #print(line1Arr[6])

                        if( line1Arr[6].find(SortLog[0]) >= 0 ):
                            SortLogIndex = 0
                            curLines+=1
                            continue

                        if(line1Arr[6].find(SortLog[1]) >= 0 ):
                            if(SortLogIndex != 0):
                                print( curLines, ":", lineList[curLines] )
                                SortLogIndex = 0
                            else:
                                SortLogIndex = 1
                            
                        curLines+=1
        print("FindSortLog1 end")


##检测指定顺序的日志,列出不一致的位置
def FindRepeatLog( log, lines, curFileName ):	
        fileEncoding = GetEncode( curFileName )
        print(fileEncoding)
            
        with open( curFileName, "r", encoding="gb2312", errors='ignore') as f:
                lineList = f.readlines() 

                curLines = 0
                TotalLines = len(lineList)
                print("TotalLines:",TotalLines)
                
                while(curLines < TotalLines):
                        #print("curLines:",curLines)
                        line1Arr = lineList[curLines].split('\t')
                        #print(line1Arr[6])

                        if( line1Arr[6].find(log) >= 0 ):
                            if( (curLines+lines) < TotalLines):
                                lineArr_ = lineList[curLines+lines].split('\t')
                                if( lineArr_[6].find(log) >= 0 ):
                                    curLines+=lines
                                    print(curFileName, ":", curLines)
                                    continue
                            
                        curLines+=1
        print("FindRepeatLog end")



def getCurrFileList(dir, suffix=''):
    '''Get all file list with specified suffix under current level folder
    Input:  
        dir     :   Current folder
        suffix  :   default to blank, means select all files.
    Output:
        File list
    '''
    if suffix=='':   
        files=glob.glob(dir)
    else:
        files=glob.glob(dir+suffix)
    flist=[]    
    for f in files:
        flist.append(os.path.join(os.getcwd(), f))
    return flist
        


if __name__ == '__main__':
        start = time.process_time()
        #print(sys.getdefaultencoding())
        #curFileName = "D:/awei/data/B10/B10_20181101/syslog/syslog_2018-11-02"
        #TimePoint = "2018_11_01_13_30_00"
        #GetFromTime( TimePoint, curFileName )

        #SortLog = ["等待VCR比对OK", "龙门去Particel:" , "下压延时超时" ]
##        SortLog[0] = "等待VCR比对OK"
##        SortLog[1] = "判断是子按压点还是主按压点，是否超出按压次数"
##        SortLog[2] = "下压延时超时"

        #FindSortLog( SortLog, curFileName+"_From_"+TimePoint )

##        fileList = getCurrFileList("D:/awei/data/B10/B10_20181101/syslog/syslog*")
##        for file in fileList:
##            print(file)
##            FindRepeatLog( "坐标入栈", 10, file )

        
##        DeleteRepeatLog( curFileName, "PLC CheckPoint ", 9, 1 )
##        GetBreakPoint( 30, curFileName+ "_edit" )
        fileList = getCurrFileList("F:/awei/tasks/B10/D/syslog")
        for file in fileList:
            GetBreakRestartPoint( 10, file )
        end = time.process_time()
        print(end-start)


	
